using UnityEngine;
using System.Collections;

public class _Player4 : MonoBehaviour {

	float f_jump;

	bool jump = false;
	
	public static float f_basejump = 0;
	public static float f_coins;
	public static bool b_gravity = true;	
	
	public Vector2 size = new Vector2(3,5);
	Vector2 realS;
	float sprite = 0;
	
	public Texture[] sprites;
	
	void Start () 
	{
		gameObject.AddComponent("MeshRenderer");
		gameObject.AddComponent("MeshFilter");
		gameObject.AddComponent("CapsuleCollider");
		
		this.gameObject.transform.position = new Vector3 (transform.position.x * Menu.ns.x , transform.position.y * Menu.ns.y , 0);
		
		realS = new Vector2(size.x * Menu.ns.x , size.y * Menu.ns.y);
		
		/*for(int i = 1 ; i <= 4 ; i++)
		{
			sprites[i] = (Texture)Resources.Load("Texturas/C_N_"+i);
			Debug.Log("C_N_"+i);
		}*/
		
		Mesh mesh = Quad.Create(realS.x ,realS.y );
		gameObject.GetComponent<MeshFilter>().mesh = mesh;
		gameObject.GetComponent<MeshRenderer>().material = (Material) Resources.Load("Outros/Transparent");
		gameObject.GetComponent<MeshRenderer>().material.mainTexture = sprites[0];
		gameObject.GetComponent<CapsuleCollider>().radius = realS.y * 0.8f;
		gameObject.GetComponent<CapsuleCollider>().height = realS.x * 2;
		gameObject.GetComponent<CapsuleCollider>().direction = 0;

		f_basejump = 0;
		_Victory4.stop = false;
		
	}
	
	void Update () 
	{
		#region gravidade, velocidade e pulo
		f_jump = f_basejump * Time.deltaTime * 2;
		f_basejump -= f_jump;
		
		
		renderer.material.mainTexture =  sprites[(int) sprite % 4];			
		#endregion

		#region sequencia de ifs para resolver fisicas e etc	
	 	if(Input.touchCount == 1 )
		{
			if(Input.touches[0].position.y < Screen.height / 2)
			{
				this.gameObject.transform.Translate (0, _Speed.speed / 1.5f , 0);
				
			}
			
			else
			{
				this.gameObject.transform.Translate (0, -_Speed.speed / 1.5f , 0);
			}
		}
		
	
			if(Input.GetKey(KeyCode.W))
			{
				this.gameObject.transform.Translate ( 0,- _Speed.speed / 1.5f , 0);
				sprite += 8 * Time.deltaTime;
			}
			
		else if(Input.GetKey(KeyCode.S))
		{
			this.gameObject.transform.Translate (  0, _Speed.speed / 1.5f , 0);
			sprite += 8 * Time.deltaTime;
		}
		
		else
		{
			sprite = 1;
		}
		
		#endregion
		
		if(_Victory4.stop == true)
		{
			transform.Translate(-_Speed.speed , 0 , 0);
		}
		
		if(transform.position.x < -2)
		{
			Application.LoadLevel ("SelecaoCenas");
		}
		
		transform.Translate(0, f_jump , 0);
	}
	
	
	void OnCollisionEnter(Collision c)
	{
		if(c.gameObject.CompareTag("ground"))
		{
			jump = true;
		}
	}
}
